package cn.nawang.ebeim.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.domain.StorageCloud;
import cn.nawang.ebeim.api.domain.StorageServer;
import cn.nawang.ebeim.api.service.StorageCloudService;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.Util;
import cn.nawang.ebeim.api.utils.ValidateUtil;

/**
 * @ClassName: StorageCloudController
 * @Description: TODO
 * @author fanyb
 * @date Aug 18, 2015 10:53:38 AM
 * @version v1.0
 */
@RestController
public class StorageCloudController {
  private static final Logger LOG = LoggerFactory.getLogger(StorageCloudController.class);

  @Autowired
  private StorageCloudService storageCloudService;

  /**
   * 
   * @author fanyb
   * @Description: 根据用户编号获取用户云存储列表
   * @param customerId 用户编号
   * @return Map<String,Object> 用户云存储列表
   * @date Aug 18, 201510:56:13 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/storage_cloud/find/customer_id", method = RequestMethod.GET)
  public Map<String, Object> findUserStorageCloudList(String customerId) {
    LOG.debug("/token/storage_cloud/find/customer_id {}" + customerId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    map.put("list", storageCloudService.findUserStorageCloudList(customerId));
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findUserStorageCloudList result:" + map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找根据存储云编号存储云列表
   * @param params 存储云编号数组
   * @return Map<String,Object> 结果
   * @date Aug 26, 201511:38:37 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/storage_cloud/find/storage_cloud_id", method = RequestMethod.GET)
  public Map<String, Object> findStorageCloudByParams(String[] storageCloudId) {
    LOG.debug("/token/storage_cloud/find/storage_cloud_id {}" + storageCloudId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    map.put("list", storageCloudService.findByParam(storageCloudId));
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findStorageCloudByParams result:" + map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据云存储名查找到云存储
   * @param name 云存储名
   * @return Map<String,Object>
   * @date Sep 16, 201511:29:10 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/storage_cloud/find/storage_cloud_name", method = RequestMethod.GET)
  public Map<String, Object> findByStorageCloudName(String name) {
    LOG.debug("/token/storage_cloud/find/storage_cloud_name {}", name);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    map.put("obj", storageCloudService.findByStorageCloudName(name));
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findByStorageCloudName result:" + map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 新增storageCloud
   * @param storageCloud
   * @return Map<String,Object>
   * @date Oct 19, 20154:40:16 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_cloud/add", method = RequestMethod.POST)
  public Map<String, Object> add(StorageCloud storageCloud) {
    LOG.debug("/storage_cloud/add {}", storageCloud);
    Map<String, Object> map = new HashMap<String, Object>();
    boolean valid = valid(map, storageCloud);
    if (!valid) {
      return map;
    }
    boolean isSuccess = storageCloudService.add(storageCloud);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
      map.put("desc", "新增成功。");
    } else {
      map.put("code", Constant.CODE_ERROR);
      map.put("desc", "服务器忙，请稍后再试。");
    }
    LOG.debug("add result:" + map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 分页列表
   * @param page
   * @param request
   * @return Object
   * @date Oct 19, 20158:23:17 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_cloud/page_list", method = RequestMethod.GET)
  public Object findAllByPaging(Pages page, HttpServletRequest request) {
    LOG.debug("/storage_cloud/list {}", page);
    Map<String, Object> map = new HashMap<String, Object>();
    // 组装查询条件
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();
    // 推送消息
    String name = request.getParameter("name");
    if (!StringUtils.isEmpty(name)) {
      dtovect.add(new SearchFormParams("sc.name", name, "name", SearchFormParams.STRING_TYPE));
    }
    if (dtovect.size() > 0) {
      page.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList pagelist = storageCloudService.findPageList(page);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pagelist.getObjList());
    map.put("page", pagelist.getPage());
    LOG.debug("findAllByPaging {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据id获取实体
   * @param id
   * @return Object
   * @date Oct 19, 20158:23:34 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_cloud/id", method = RequestMethod.GET)
  public Object findOne(String id) {
    LOG.debug("/storage_cloud/id {}", id);
    Map<String, Object> map = new HashMap<String, Object>();
    StorageCloud storageCloud = storageCloudService.findOne(id);
    if (storageCloud == null) {
      map.put("code", Constant.CODE_ERROR);
    } else {
      map.put("code", Constant.CODE_SUCCESS);
      map.put("obj", storageCloud);
    }
    LOG.debug("findOne {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 批量删除
   * @param ids
   * @return Object
   * @date Oct 19, 20158:23:50 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_cloud/delete", method = RequestMethod.GET)
  public Object delete(String[] ids) {
    LOG.debug("/storage_cloud/delete ids" + ids);
    Map<String, Object> map = new HashMap<String, Object>();
    boolean isSuccess = storageCloudService.delete(ids);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
    } else {
      map.put("code", Constant.CODE_ERROR);
    }
    LOG.debug("delete {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新
   * @param storageCloud
   * @return Object
   * @date Oct 19, 20158:24:02 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_cloud/update", method = RequestMethod.GET)
  public Object update(StorageCloud storageCloud) {
    LOG.debug("/storage_cloud/update {}", storageCloud);
    Map<String, Object> map = new HashMap<String, Object>();
    boolean valid = valid(map, storageCloud);
    if (!valid) {
      return map;
    }
    boolean isSuccess = storageCloudService.update(storageCloud);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
      map.put("desc", "更新成功。");
    } else {
      map.put("code", Constant.CODE_ERROR);
      map.put("desc", "服务器忙，请稍后再试。");
    }
    LOG.debug("update {}", map);
    return map;
  }

  public boolean valid(Map<String, Object> resultMap, StorageCloud storageCloud) {
    if (Util.isBlank(storageCloud.getName())) {
      resultMap.put("desc", "请输入云存储名称。");
      return false;
    }
    if (Util.isBlank(storageCloud.getSpaceCapacity())) {
      resultMap.put("desc", "请输入总空间。");
      return false;
    }
    if (Util.isBlank(storageCloud.getSpaceUsage())) {
      resultMap.put("desc", "请输入已用空间。");
      return false;
    }
    if (!Util.isPositiveIntegers(String.valueOf(storageCloud.getSpaceCapacity()))) {
      resultMap.put("desc", "总空间只允许输入正整数。");
      return false;
    }
    if (!Util.isPositiveDouble(String.valueOf(storageCloud.getSpaceUsage()))) {
      resultMap.put("desc", "总空间只允许输入正整数。");
      return false;
    }
    if (storageCloud.getSpaceCapacity() < storageCloud.getSpaceUsage()) {
      resultMap.put("desc", "总空间不允许小于已用空间。");
      return false;
    }

    StorageCloud sc = storageCloudService.findByStorageCloudName(storageCloud.getName());
    if (!ValidateUtil.isNull(sc)) {
      if (ValidateUtil.isEmpty(storageCloud.getId())) {
        resultMap.put("desc", "很抱歉,您输入的云存储已经存在，请勿重复添加。");
        return false;
      } else {
        if (!sc.getId().equals(storageCloud.getId())) {
          resultMap.put("desc", "很抱歉,您输入的云存储和端口已经存在，请勿重复添加。");
          return false;
        }
      }
    }
    return true;
  }
}
